package com.example.elderlycare.service.Impl;

import com.example.elderlycare.dao.entity.Roster;
import com.example.elderlycare.service.entity.TemplateItem;
import com.example.elderlycare.utils.DateUtil;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class RosterGenerateByTemplate implements BaseRoster {
    @Override
    public List<Roster> generateRosterRecord(Map<String, Object> params) {
        List<TemplateItem> result = readTemplate((MultipartFile) params.get("file"));
        if (result == null) {
            return null;
        }
        return result.stream().map(item -> {
            try {
                return Roster.builder().employeeId((int) Double.parseDouble("".equals(item.getId())?"0":item.getId())).
                        dutyAt(DateUtil.getDateFromStringNew(item.getDuty())).
                        type((int) Double.parseDouble("".equals(item.getType())?"0":item.getType())).
                        absent((int) Double.parseDouble("".equals(item.getAbsent())?"0":item.getAbsent())).
                        isLeave((int) Double.parseDouble("".equals(item.getLeave())?"0":item.getLeave())).build();
            } catch (ParseException e) {
                e.printStackTrace();
                return null;
            }
        }).collect(Collectors.toList());
    }

    public static List<TemplateItem> readTemplate(MultipartFile file) {
        try {
            List<TemplateItem> readItems = new ArrayList<>();
            //创建工作簿对象
            XSSFWorkbook xssfWorkbook = new XSSFWorkbook(file.getInputStream());
            //获取工作簿下sheet的个数
            int sheetNum = xssfWorkbook.getNumberOfSheets();
            if (sheetNum <= 0) {
                return null;
            }

            XSSFSheet sheet = xssfWorkbook.getSheetAt(0);

            int maxRow = sheet.getLastRowNum();
            if (maxRow < 1 || sheet.getRow(0).getLastCellNum() != 6) {
                return null;
            }
            for (int row = 1; row <= maxRow; row++) {
                TemplateItem.TemplateItemBuilder templateItemBuilder = TemplateItem.builder();
                templateItemBuilder.id(String.valueOf(sheet.getRow(row).getCell(0))).
                        name(String.valueOf(sheet.getRow(row).getCell(1))).
                        duty(String.valueOf(sheet.getRow(row).getCell(2))).
                        type(String.valueOf(sheet.getRow(row).getCell(3))).
                        absent(String.valueOf(sheet.getRow(row).getCell(4))).
                        leave(String.valueOf(sheet.getRow(row).getCell(5)));
                readItems.add(templateItemBuilder.build());
            }
            return readItems;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}